import pandas as pd


def gene_sld(p_type, color):
    print("开始生成{}样式文件", p_type)
    line_content = ""
    point_content = ""
    with open("data/line.xml", 'r', encoding='utf-8') as lineXml:
        line_content = lineXml.read()
    line_content = line_content.replace("${type}", p_type)
    line_content = line_content.replace("${color}", color)

    with open("data/point.xml", 'r', encoding='utf-8') as lineXml:
        point_content = lineXml.read()
    point_content = point_content.replace("${type}", p_type)
    point_content = point_content.replace("${color}", color)

    # 保存最终的 SLD 文件
    with open("out/" + p_type + '_point.sld', 'w', encoding='UTF-8') as point_out:
        point_out.write(point_content)
    with open("out/" + p_type + '_line.sld', 'w', encoding='UTF-8') as line_out:
        line_out.write(line_content)
    print("开始生成{}样式文件完毕", p_type)


if __name__ == '__main__':
    # 读取 Excel 文件中关于图层名称和颜色的配置
    excel_file = '重庆.xlsx'
    df = pd.read_excel(excel_file)
    # 遍历每一行
    for index, row in df.iterrows():
        p_type = row['p_type']
        color = row['Color']
        gene_sld(p_type, color)
